﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Controls;
namespace PrintDrawing.BaseClassDraw
{
    /// <summary>
    /// lớp quản lý cấu hình trang in
    /// </summary>
    internal class ConfigPrintLaze
    {
        //PrivateConst node name XLM
        private const string PRINT_NAME = "Print";
        private const string CONFIG_NAME = "Config";
        private const string ORIENTATION = "Orientation";
        private const string PAGES_NAME = "Pages";
        //private counst Atribute name XLM 
        //for <Print/>
        private const string PRINT_STYLE_PAGE_NAME = "StylePage";
        private const string PRINT_HEIGHT = "Height";
        private const string PRINT_WIDTH = "Width";
        private const string PRINT_TITEL = "Titel";
        private const string PRINT_COUNTPAGE = "CountPage";

        public const string ORIENTATION_PORTRAINT = "PORTRAINT";
        public const string ORIENTATION_LANDSCAPE = "LANDSCAPE";


        private string _orientation = "PORTRAINT";

        public string Orientation
        {
            get { return _orientation; }
            set { _orientation = value; }
        }
        private string _titel = ""; // tiêu đề trang tin
        public string Titel
        {
            get { return _titel; }
            set { _titel = value; }
        }

        private int _countPage = 0; // Tổng số trang in

        public int CountPage
        {
            get { return Pages.Count; }          
        }

        //private string _header = ""; // header trang tin
        //public string Header
        //{
        //    get { return _header; }
        //    set { _header = value; }
        //}

        private string _stylePage = ""; //kiểu trang in
        public string StylePage
        {
            get { return _stylePage; }
            set { _stylePage = value; }
        }

        private double _height = 0; // chiều cao trang in
        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }

        private double _width = 0; // chiều dài trang in
        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private List<PageInfo> _pages = new List<PageInfo>(); // các trang nội tại
        public List<PageInfo> Pages
        {
            get { return _pages; }
            set { _pages = value; }
        }

        #region MethodConfigPrintLaze

        /// <summary>
        /// Saves the XMLDOC. từ class
        /// </summary>
        /// <param name="cf">The cf.</param> trang in cần lưu
        /// <returns>XMLDOCUMENT</returns>
        public XmlDocument ImportToXmlDoc(String FullFileName)
        {
            XmlDocument doc =new XmlDocument();            
            XmlElement root = doc.CreateElement(PRINT_NAME);
            doc.AppendChild(root);

            // tạo thẻ config 
            XmlNode Config = doc.CreateElement(CONFIG_NAME);                       
            // tạo các atrribyte
            //style page
            XmlAttribute AtrConfig = doc.CreateAttribute(PRINT_STYLE_PAGE_NAME);          
            AtrConfig.Value = this.StylePage;
            Config.Attributes.Append(AtrConfig);
            //CountPage
            AtrConfig = doc.CreateAttribute(PRINT_COUNTPAGE);             
            AtrConfig.Value = this.ToString();
            Config.Attributes.Append(AtrConfig);
            //titel
            AtrConfig = doc.CreateAttribute(PRINT_TITEL);            
            AtrConfig.Value = this.Titel;
            Config.Attributes.Append(AtrConfig);
            //height
            AtrConfig = doc.CreateAttribute(PRINT_HEIGHT);            
            AtrConfig.Value = this.Height.ToString();
            Config.Attributes.Append(AtrConfig);
            //Width
            AtrConfig = doc.CreateAttribute(PRINT_WIDTH);            
            AtrConfig.Value = this.Width.ToString();
            Config.Attributes.Append(AtrConfig);

            //Orientation
            AtrConfig = doc.CreateAttribute(ORIENTATION);            
            AtrConfig.Value = this.Orientation;
            Config.Attributes.Append(AtrConfig);

            root.AppendChild(Config);
            //--------------------- XONG THẺ CONFIG//
            // tạo thẻ pages            
            XmlNode Pages = doc.CreateElement(PAGES_NAME);
            /// danh sách các page
            int curentpage = 1;
            foreach (PageInfo p in this.Pages)
            {
                Pages.AppendChild(p.ExportElementToXmlNode(doc));
            }
            root.AppendChild(Pages);
            doc.Save(new XmlTextWriter(FullFileName, System.Text.Encoding.UTF8));
            return doc;            
        }

        /// <summary>
        /// Xuất dữ liệu từ XML vào class
        /// </summary>
        /// <param name="doc"></param>
        /// <returns>Đối tượng printLaze</returns>
        public static ConfigPrintLaze ExportXmlToClass(XmlDocument doc)
        {
            ConfigPrintLaze print = new ConfigPrintLaze();
            try
            {
                //get root=<print>
                XmlNode root = doc.ChildNodes[1];

                if (root.Name == PRINT_NAME)
                {
                    ///search with node ElementNode
                    foreach (XmlNode d in root.ChildNodes)
                    {
                        //d is config
                        if (d.Name == CONFIG_NAME)
                        {
                            /// search with attribute
                            foreach (XmlAttribute a in d.Attributes)
                            {
                                //if (a.Name == PRINT_COUNTPAGE)
                                //    print.CountPage = int.Parse(a.Value);
                                if (a.Name == PRINT_HEIGHT)
                                    print.Height = double.Parse(a.Value);
                                if (a.Name == PRINT_TITEL)
                                    print.Titel = a.Value;
                                if (a.Name == PRINT_WIDTH)
                                    print.Width = double.Parse(a.Value);
                                if (a.Name == PRINT_STYLE_PAGE_NAME)
                                    print.StylePage = a.Value;
                                if (a.Name == ORIENTATION)
                                    print.Orientation = a.Value;
                            }
                        }
                        // d is Pages
                        if (d.Name == PAGES_NAME)
                        {
                            print.Pages = PageInfo.ExportXmlToClass(d);
                        }
                    }
                }
                return print;
            }
            catch(Exception ex)
            {
                throw ex;
                return null;
            }
        }


        #endregion
    }
}
